package com.cennavi.mineplay.metadata;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.cennavi.mineplay.utils.DBUtils;

@RestController
@RequestMapping(value = "/metadata")
public class Query {

	
	@Autowired
	protected JdbcTemplate jdbc;
	
	@Value("${webservice.prefix}")
	private String serviceUrlfPrefix;
	
	@RequestMapping(value = "/table_list")
	public Object tableList(String user_id) {
		String sql = "select * from table_list";
		
		if (user_id != null && user_id.length()>0){
			sql += " where user_id='"+user_id+"'";
		}

		try {
			
			List<Map<String, Object>> list = jdbc.queryForList(sql);
			
			return list;

		} catch (Exception e) {
			e.printStackTrace();
		}

		return null;
	}
	
 
	@RequestMapping(value = "/render_model_list")
	public Object renderModelList(String table_name) {
		
		String geomSql = "select st_astext(geom) as wkt from "+ table_name +" limit 1";
		
		List<Map<String, Object>> listGeomType = jdbc.queryForList(geomSql);
		
		if (listGeomType == null || listGeomType.size()==0)
			return new ArrayList<String>();
		
		String wkt = listGeomType.get(0).toString();
		
		String geom_type = null;
		
		if (wkt.indexOf("POINT")>=0)
			geom_type= "point";
		else if (wkt.indexOf("LINE")>=0)
			geom_type = "line";
		else
			geom_type = "polygon";
		
		String sql = "select * from render_model_list where geom_type=?";

		try {
			
			List<Map<String, Object>> list = jdbc.queryForList(sql,geom_type);
			
			for(Map<String,Object> map : list){
				
				
				map.put("image_url", serviceUrlfPrefix+"/image/render/"+map.get("image_url"));
				if (map.get("service_url")==null){
					String serviceType = map.get("service_type").toString();
					String type = map.get("type").toString();
					if ("particleEffect".equals(type)){
						map.put("service_url", serviceUrlfPrefix+"/data/pbf_particle/{z}/{x}/{y}");
					}else if ("hexagon".equals(type)){
						map.put("service_url", serviceUrlfPrefix+"/data/pbf/hexgrid/{z}/{x}/{y}");
					}else if ("grid".equals(type)){
						map.put("service_url", serviceUrlfPrefix+"/data/pbf/grid/{z}/{x}/{y}");
					}else if ("grid_num".equals(type)){
						map.put("service_url", serviceUrlfPrefix+"/data/pbf/grid_num/{z}/{x}/{y}");
					}else if ("PBF".equals(serviceType)){
						map.put("service_url", serviceUrlfPrefix+"/data/pbf/{z}/{x}/{y}");
					}else if ("GEOJSON".equals(serviceType)){
						map.put("service_url", serviceUrlfPrefix+"/data/geojson");
					}
				}else{
					map.put("service_url", serviceUrlfPrefix+map.get("service_url"));
				}
			}
			
			return list;

		} catch (Exception e) {
			e.printStackTrace();
		}

		return null;
	}
	
	@RequestMapping(value = "/chart_model_list")
	public Object chartModelList() {
		String sql = "select * from chart_model_list";

		try {
			
			List<Map<String, Object>> list = jdbc.queryForList(sql);
			
			for(Map<String,Object> map : list){
				map.put("group", map.get("cgroup"));
				map.remove("cgroup");
				map.put("image_url", serviceUrlfPrefix+"/image/chart/"+map.get("image_url"));
			}
			
			return list;

		} catch (Exception e) {
			e.printStackTrace();
		}

		return null;
	}
	
	@RequestMapping(value = "/facade_model_list")
	public Object facadeModelList() {
		String sql = "select * from facade_model_list";

		try {
			
			List<Map<String, Object>> list = jdbc.queryForList(sql);
			for(Map<String,Object> map : list){
				map.put("image_url", serviceUrlfPrefix+"/image/facade/"+map.get("image_url"));
			}
			return list;

		} catch (Exception e) {
			e.printStackTrace();
		}

		return null;
	}
	
	@RequestMapping(value = "/table_meta")
	public Object getTableMetaInfo(String name){
		
		try {
			Connection conn = jdbc.getDataSource().getConnection();
			
			Object obj = DBUtils.getMetaInfoByTableName(name, conn);
			
			conn.close();
			
			return obj;
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return null;
		
	}
	
	
	@RequestMapping(value = "/table_construct")
	public Object getTableConstruct(String name){
		
		String sql = "SELECT a.attnum, a.attname AS field, t.typname AS type FROM pg_class c, pg_attribute a LEFT OUTER JOIN pg_description b ON a.attrelid=b.objoid AND a.attnum = b.objsubid, pg_type t WHERE c.relname = ? and a.attnum > 0 and a.attrelid = c.oid and a.atttypid = t.oid ORDER BY a.attnum";
		
		return jdbc.queryForList(sql, name);
		
	}
	
	
}
